#!/usr/bin/env python
# -*- coding:utf8 -*-
# uncompyle6 version 2.15.1
# Python bytecode 2.7 (62211)
# Decompiled from: Python 2.7.10 (default, Jul  1 2017, 13:36:56) 
# [GCC 4.4.6 20110731 (Red Hat 4.4.6-4)]
# Embedded file name: ./esb/components/bk/apis/fta/http_relay.py
# Compiled at: 2017-11-16 15:44:28
from django import forms
from common.forms import BaseComponentForm
from components.component import Component
from common.constants import API_TYPE_OP
from .toolkit import tools, configs

class HttpRelay(Component):
    """[FTA] 外网请求代理
    
    {% block api_doc %}
    
    描述
    ~~~~
    
    参数说明
    ~~~~~~~~
    
    {{ common_args_desc }}
    
    其他参数
    
    ===============  ======  ========  ===========================================
    参数名称         必须    类型      参数说明
    ===============  ======  ========  ===========================================
    method           Y       string    请求方法（GET, POST)
    url              Y       int       请求URL
    kwargs           N       kwargs    其他requests支持的参数
    ===============  ======  ========  ===========================================
    
    请求参数示例
    ~~~~~~~~~~~~
    
    .. code:: json
    
        {
            "app_id": "46",
            "method": "GET",
            "url": 'http',
            "data": '',
        }
    
    结果说明
    ~~~~~~~~
    
    .. code:: json
    
        {
            "result": true,
            "code": "00",
            "message": "",
            "data": ''
        }
    
    {% endblock %}
    """
    sys_name = configs.SYSTEM_NAME
    api_type = API_TYPE_OP

    class Form(BaseComponentForm):
        app_id = forms.CharField(label=u'业务ID', required=True)
        method = forms.CharField(label=u'请求方法', required=True)
        url = forms.CharField(label=u'请求URL', required=True)
        kwargs = forms.CharField(label=u'请求数据', required=False)

        def clean_kwargs(self):
            return self.data.get('kwargs') or {}

    def handle(self):
        client = tools.HttpClient()
        method = self.form_data['method']
        url = self.form_data['url']
        kwargs = self.form_data['kwargs']
        result = client.request(method, url, **kwargs)
        self.response.payload = result
